<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div [formGroup]="datasourcesFormGroup" class="tb-form-panel tb-datasources" [class.no-padding-bottom]="!showAddDatasource">
  <div class="flex flex-col">
    <div class="flex flex-row items-center justify-between">
      <div class="tb-form-panel-title">{{ (singleDatasource ? (isAlarmSource ? 'widget-config.alarm-source' : 'widget-config.datasource') : 'widget-config.datasources') | translate }}</div>
      <div class="flex flex-1 flex-row items-center justify-center" *ngIf="timeseriesKeyError">
        <mat-error >{{ 'widget-config.timeseries-key-error' | translate }}</mat-error>
      </div>
      <tb-toggle-select *ngIf="basicMode && !hideDatasourcesMode" [ngModel]="datasourcesMode" (ngModelChange)="datasourcesModeChange($event)"
                        [ngModelOptions]="{ standalone: true }">
        <tb-toggle-option [value]="datasourceType.device">{{ 'device.device' | translate }}</tb-toggle-option>
        <tb-toggle-option [value]="datasourceType.entity">{{ 'entity.entity-alias' | translate }}</tb-toggle-option>
      </tb-toggle-select>
    </div>
    <div *ngIf="maxDatasources > 1"
         class="tb-form-panel-hint">{{ 'widget-config.maximum-datasources' | translate:{count: maxDatasources} }}</div>
  </div>
  <div *ngIf="datasourcesFormArray.length === 0; else datasourcesTemplate">
                <span translate
                      class="tb-prompt flex items-center justify-center">datasource.add-datasource-prompt</span>
  </div>
  <ng-template #datasourcesTemplate>
    <div style="overflow: auto;">
      <mat-list class="tb-drop-list"
                cdkDropList
                cdkDropListOrientation="vertical"
                (cdkDropListDropped)="onDatasourceDrop($event)"
                [cdkDropListDisabled]="dragDisabled"
                formArrayName="datasources">
        <mat-list-item *ngFor="let datasourceControl of datasourcesControls; trackBy: trackByDatasource; let $index = index;"
                       class="tb-datasource-list-item tb-draggable" cdkDrag
                       [cdkDragDisabled]="dragDisabled">
          <div class="tb-datasource-row">
            <div *ngIf="!singleDatasource" class="tb-datasource-index-container">
              <div class="tb-datasource-index">{{$index + 1}}</div>
            </div>
            <div class="tb-datasource-params">
              <div class="tb-datasource-item">
                <tb-datasource [formControl]="datasourceControl">
                </tb-datasource>
                <div *ngIf="!singleDatasource && !disabled" class="tb-datasource-buttons">
                  <button class="tb-box-button" type="button"
                          mat-icon-button
                          (click)="removeDatasource($index)"
                          matTooltip="{{ 'widget-config.remove-datasource' | translate }}"
                          matTooltipPosition="above">
                    <mat-icon>delete</mat-icon>
                  </button>
                  <button *ngIf="!dragDisabled"
                          class="tb-box-button"
                          mat-icon-button
                          type="button"
                          cdkDragHandle
                          matTooltip="{{ 'action.drag' | translate }}"
                          matTooltipPosition="above">
                    <mat-icon>drag_indicator</mat-icon>
                  </button>
                </div>
              </div>
              <tb-error class="tb-datasource-error" [error]="datasourceError[$index] ? datasourceError[$index] : ''"></tb-error>
            </div>
          </div>
        </mat-list-item>
      </mat-list>
    </div>
  </ng-template>
  <div *ngIf="showAddDatasource">
    <button type="button"
            mat-stroked-button color="primary"
            (click)="addDatasource()"
            matTooltip="{{ 'widget-config.add-datasource' | translate }}"
            matTooltipPosition="above">
      <span translate>widget-config.add-datasource</span>
    </button>
  </div>
</div>
